<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 上传附件类
 * @property Attach_model $Attach_model
 * @author liubin-bt
 *
 */
class Attach extends Base {
    
    public function __construct(){
        parent::__construct();
        $this->load->model('Attach_model');
    }
    
    /**
     * 上传附件
     */
    public function doUpload(){
        $data = $this->single_upload('file');
        $id = $this->saveProjectAttach($data);
        if( !$id ){
            $this->outJsonData(-1, '上传失败');
        }
        $result = [
            'id'=>$id,
            'filename'=>$data['orig_name'],
            'url'=>'http://'.$_SERVER['HTTP_HOST'].'/uploads/'.$data['upload_path'],
            'ext'=>str_replace('.', '', $data['file_ext']),
            'size'=>$data['file_size'],
        ];
        $this->outJsonData(0, $result);
    }
    
    private function saveProjectAttach($data){
        $insertdb = [
            'filename'=>$data['orig_name'],
            'url'=>$data['upload_path'],
            'size'=>$data['file_size'],
            'ext'=>str_replace('.', '', $data['file_ext']),
            'ctime'=>date('Y-m-d H:i:s')
        ];
        $res = $this->Attach_model->insert($insertdb);
        return $res;
    }
}
?>